function toasts(msg) {
        $.Toast(msg, "", "success", {
            appendTo: "body",
            stack: false,
            has_icon:true,
            has_close_btn:true,
            fullscreen:false,
            position_class: "toast-top-right",
            timeout:5000,
            width: 500,
            sticky:false,
            has_progress:true,
            rtl:false,
        });
    }

var dappContactAddress = "n1j1hF1wiMZNjwUpw1VqDhUp3bnaXqWbq8X";
var nebulas = require("nebulas"),
    Account = Account,
    neb = new nebulas.Neb();
var NebPay = require("nebpay"); // https://github.com/nebulasio/nebPay
var nebPay = new NebPay();
neb.setRequest(new nebulas.HttpRequest("https://mainnet.nebulas.io"));

var address = "";

try {
	if (webExtensionWallet) {
	   //alert("您已安装WebExtensionWallet插件");
	}
} catch(e) {
	alert("您未安装WebExtensionWallet，为了不影响您的使用，请安装插件");
}
if (typeof nebPay.simulateCall == "undefined" || typeof nebPay.call == "undefined") {
	alert('检测到你未开启星云钱包插件,请启动星云钱包插件并刷新.');
}

$(document).ready(function() {
    window.postMessage({
        "target": "contentscript",
        "data": {},
        "method": "getAccount",
    }, "*");
});

var serialNumber;

$("#check_btn").click(function() {
    if (!$("#search_no").val()) {
        alert('请输入要验证的RMB编号');
        return;
    }

    var from = dappContactAddress;
    var value = "0";
    var nonce = "0";
    var gas_price = "1000000";
    var gas_limit = "2000000";
    var callFunction = "check";
    var NO = $("#search_no").val();
    var callArgs = "[\"" + NO + "\"]";
    console.log(callArgs);
    var contract = {
        "function": callFunction,
        "args": callArgs
    }
    toasts('正在检索，请稍候！');

    neb.api.call(from, dappContactAddress, value, nonce, gas_price, gas_limit, contract).then(function(resp) {
        console.log(resp);
        var result = resp.result;
        
        if (result === 'null') {
            alert("此编号无人报告为假币！");
            return;
        }
        console.log(result);
        result = JSON.parse(result);

        alert('编号"' + result.NO + '"于' + result.foundDate + '被报告为假币，请警惕！')
    }).
    catch (function(err) {
        console.log("error :" + err.message);
    })

})

$('#commit_btn').click(function() {
    if (!$("#commit_no").val() ) {
       alert('请填写假币编号');
       return;
    }
    
    var to = dappContactAddress;
    var value = "0";
    var callFunction = "commit";
    var posts = $("#commit_no").val();
    
    var callArgs = "[\"" + posts + "\"]";

    serialNumber = nebPay.call(to, value, callFunction, callArgs, { // 使用nebpay的call接口去调用合约,
        listener: function(resp) {
            if (resp.txhash) {
                alert('提交成功,请稍后刷新查询');
            } else {
                alert('信息添加失败,请稍后再试');
            }
            console.log("thecallback is " + resp)
        }
    });
})